@file:Suppress("unused")

package io.legado.app.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Service
import android.content.*
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.BatteryManager
import android.provider.Settings
import android.widget.Toast
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.content.edit
//import androidx.preference.PreferenceManager
//import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import io.legado.app.R
import io.legado.app.constant.AppConst
import java.io.File
import java.io.FileOutputStream

inline fun <reified A : Activity> Context.startActivity(configIntent: Intent.() -> Unit = {}) {
    val intent = Intent(this, A::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    intent.apply(configIntent)
    startActivity(intent)
}

inline fun <reified T : Service> Context.startService(configIntent: Intent.() -> Unit = {}) {
    startService(Intent(this, T::class.java).apply(configIntent))
}

inline fun <reified T : Service> Context.stopService() {
    stopService(Intent(this, T::class.java))
}

//fun Context.toastOnUi(message: Int) {
//    runOnUI {
//        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
//    }
//}
//
//fun Context.toastOnUi(message: CharSequence?) {
//    runOnUI {
//        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
//    }
//}
//
//fun Context.longToastOnUi(message: Int) {
//    runOnUI {
//        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
//    }
//}
//
//fun Context.longToastOnUi(message: CharSequence?) {
//    runOnUI {
//        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
//    }
//}
//
//val Context.defaultSharedPreferences: SharedPreferences
//    get() = PreferenceManager.getDefaultSharedPreferences(this)
//
//fun Context.getPrefBoolean(key: String, defValue: Boolean = false) =
//    defaultSharedPreferences.getBoolean(key, defValue)
//
//fun Context.putPrefBoolean(key: String, value: Boolean = false) =
//    defaultSharedPreferences.edit { putBoolean(key, value) }
//
//fun Context.getPrefInt(key: String, defValue: Int = 0) =
//    defaultSharedPreferences.getInt(key, defValue)
//
//fun Context.putPrefInt(key: String, value: Int) =
//    defaultSharedPreferences.edit { putInt(key, value) }
//
//fun Context.getPrefLong(key: String, defValue: Long = 0L) =
//    defaultSharedPreferences.getLong(key, defValue)
//
//fun Context.putPrefLong(key: String, value: Long) =
//    defaultSharedPreferences.edit { putLong(key, value) }
//
//fun Context.getPrefString(key: String, defValue: String? = null) =
//    defaultSharedPreferences.getString(key, defValue)
//
//fun Context.putPrefString(key: String, value: String?) =
//    defaultSharedPreferences.edit { putString(key, value) }
//
//fun Context.getPrefStringSet(
//    key: String,
//    defValue: MutableSet<String>? = null
//): MutableSet<String>? = defaultSharedPreferences.getStringSet(key, defValue)
//
//fun Context.putPrefStringSet(key: String, value: MutableSet<String>) =
//    defaultSharedPreferences.edit { putStringSet(key, value) }
//
//fun Context.removePref(key: String) =
//    defaultSharedPreferences.edit { remove(key) }


fun Context.getCompatColor(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)

fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? = ContextCompat.getDrawable(this, id)

fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? =
    ContextCompat.getColorStateList(this, id)

/**
 * 系统息屏时间
 */
val Context.sysScreenOffTime: Int
    get() {
        var screenOffTime = 0
        try {
            screenOffTime =
                Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return screenOffTime
    }

val Context.statusBarHeight: Int
    get() {
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        return resources.getDimensionPixelSize(resourceId)
    }

val Context.navigationBarHeight: Int
    get() {
        val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return resources.getDimensionPixelSize(resourceId)
    }

//fun Context.share(text: String, title: String = getString(R.string.share)) {
//    kotlin.runCatching {
//        val intent = Intent(Intent.ACTION_SEND)
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        intent.putExtra(Intent.EXTRA_SUBJECT, title)
//        intent.putExtra(Intent.EXTRA_TEXT, text)
//        intent.type = "text/plain"
//        startActivity(Intent.createChooser(intent, title))
//    }
//}

//@SuppressLint("SetWorldReadable")
//fun Context.shareWithQr(
//    text: String,
//    title: String = getString(R.string.share),
//    errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.H
//) {
//    val bitmap = QRCodeUtils.createQRCode(text, errorCorrectionLevel = errorCorrectionLevel)
//    if (bitmap == null) {
//        toastOnUi(R.string.text_too_long_qr_error)
//    } else {
//        try {
//            val file = File(externalCacheDir, "qr.png")
//            val fOut = FileOutputStream(file)
//            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut)
//            fOut.flush()
//            fOut.close()
//            file.setReadable(true, false)
//            val contentUri = FileProvider.getUriForFile(this, AppConst.authority, file)
//            val intent = Intent(Intent.ACTION_SEND)
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            intent.putExtra(Intent.EXTRA_STREAM, contentUri)
//            intent.type = "image/png"
//            startActivity(Intent.createChooser(intent, title))
//        } catch (e: Exception) {
//            toastOnUi(e.localizedMessage ?: "ERROR")
//        }
//    }
//}
//
//fun Context.sendToClip(text: String) {
//    val clipboard =
//        getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
//    val clipData = ClipData.newPlainText(null, text)
//    clipboard?.let {
//        clipboard.setPrimaryClip(clipData)
//        longToastOnUi(R.string.copy_complete)
//    }
//}

fun Context.getClipText(): String? {
    val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
    clipboard?.primaryClip?.let {
        if (it.itemCount > 0) {
            return it.getItemAt(0).text.toString().trim()
        }
    }
    return null
}

//fun Context.sendMail(mail: String) {
//    try {
//        val intent = Intent(Intent.ACTION_SENDTO)
//        intent.data = Uri.parse("mailto:$mail")
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        startActivity(intent)
//    } catch (e: Exception) {
//        toastOnUi(e.localizedMessage ?: "Error")
//    }
//}

/**
 * 系统是否暗色主题
 */
fun Context.sysIsDarkMode(): Boolean {
    val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
    return mode == Configuration.UI_MODE_NIGHT_YES
}

/**
 * 获取电量
 */
val Context.sysBattery: Int
    get() {
        val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val batteryStatus = registerReceiver(null, iFilter)
        return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
    }

val Context.externalFiles: File
    get() = this.getExternalFilesDir(null) ?: this.filesDir

val Context.externalCache: File
    get() = this.externalCacheDir ?: this.cacheDir

//fun Context.openUrl(url: String) {
//    openUrl(Uri.parse(url))
//}
//
//fun Context.openUrl(uri: Uri) {
//    val intent = Intent(Intent.ACTION_VIEW)
//    intent.data = uri
//    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//    if (intent.resolveActivity(packageManager) != null) {
//        try {
//            startActivity(intent)
//        } catch (e: Exception) {
//            toastOnUi(e.localizedMessage ?: "open url error")
//        }
//    } else {
//        try {
//            startActivity(Intent.createChooser(intent, "请选择浏览器"))
//        } catch (e: Exception) {
//            toastOnUi(e.localizedMessage ?: "open url error")
//        }
//    }
//}

val Context.channel: String
    get() {
        try {
            val pm = packageManager
            val appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
            return appInfo.metaData.getString("channel") ?: ""
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

